package com.jtbd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jtbd.domain.UserInfo;
import com.jtbd.domain.UserStudent;
import com.jtbd.dto.db.UserStudentInfo;
import com.jtbd.mapper.UserInfoMapper;
import com.jtbd.mapper.UserStudentMapper;
import com.jtbd.service.UserInfoService;
import com.jtbd.service.UserStudentService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

@Service
public class UserStudentServiceImpl implements UserStudentService {
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserStudentMapper userStudentMapper;
    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public UserStudentInfo getUserStudentInfo(Long uid) {
        LambdaQueryWrapper<UserStudent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserStudent::getUid, uid);
        UserStudent userStudent = userStudentMapper.selectOne(queryWrapper);
        if (userStudent != null) {
            LambdaQueryWrapper<UserInfo> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(UserInfo::getId, userStudent.getUid());
            UserInfo userInfo = userInfoMapper.selectOne(queryWrapper2);
            return new UserStudentInfo(userInfo, userStudent);
        }
        return null;
    }

    @Override
    @Transactional
    public int addUserStudentInfo(UserStudentInfo userStudentInfo) {
        Long addUid = userInfoService.addUserInfo(userStudentInfo.getUserInfo());
        if (!ObjectUtils.isEmpty(addUid)) {
            userStudentInfo.getUserStudent().setUid(addUid);
            return userStudentMapper.insert(userStudentInfo.getUserStudent());
        }
        return 0;
    }
}
